package com.amkai.aes.webservice.binary.objecthandler.binarymanager;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.Serializable;

import org.apache.log4j.Logger;

import com.amkai.aes.aescommon.binary.BinaryManagerException;
import com.amkai.aes.aescommon.service.binarymanager.BinaryManagerJob;
import com.amkai.aes.aescommon.service.binarymanager.BinaryManagerResult;
import com.amkai.aes.aescommon.service.diee.DIEECommonUtils;
import com.amkai.aes.generatedbeans.binarymgr.BinaryData;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;
import com.amkai.extensions.server.services.IBinaryManager;
import com.amkai.extensions.server.services.ServerServiceManager;

public class BinaryManagerHandler implements JobObjectHandler<BinaryManagerJob, Serializable, BinaryManagerResult> {
	
	private final Logger getLogger() {
		return Logger.getLogger(BinaryManagerHandler.class);
	}

	@Override
	public BinaryManagerResult process(BinaryManagerJob job, Serializable object) throws Exception {
		BinaryManagerResult result = new BinaryManagerResult();
		try {
			IBinaryManager bm = ServerServiceManager.getInstance().getBinaryManager();
			if (bm!=null) {
				if (job.getKind()!=null) {
					switch (job.getKind()) {
					case DOWNLOAD_DATA:
						if (job.getBinaryData()!=null) {
							SessionManager sm = SiteManager.Start();
							sm.openSession();
							BinaryData data = sm.get(BinaryData.class, job.getBinaryData().getId());
							if (data!=null) {
								File file = bm.downloadBinary(data);
								if (file!=null) {
									result.setData(DIEECommonUtils.loadFileToBuffer(file));
								}
							} else {
								throw new BinaryManagerException("No binary data with the given id: " + job.getBinaryData().getId());
							}
							sm.closeSession();
						} else {
							throw new BinaryManagerException("No binary data in job");
						}
						break;
					case UPLOAD_DATA:
						byte[] data = job.getData();
						if (data!=null) {
							if (job.getName()!=null) {
								result.setBinaryData(bm.uploadBinary(new ByteArrayInputStream(data), job.getName(), job.getSize()));
							} else {
								throw new BinaryManagerException("Data name is not specified");
							}
						} else {
							throw new BinaryManagerException("No data to upload");
						}
						break;
					case UPDATE_DATA:
						if (job.getBinaryData()!=null) {
							data = job.getData();
							if (data!=null) {
								bm.updateBinary(job.getBinaryData(), new ByteArrayInputStream(data), job.getSize());
							} else {
								throw new BinaryManagerException("No data to upload");
							}
						} else {
							throw new BinaryManagerException("No binary data in job");
						}
						break;
					case DELETE_DATA:
						if (job.getBinaryData()!=null) {
							SessionManager sm = SiteManager.Start();
							sm.openSession();
							bm.deleteBinary(sm, job.getBinaryData(), false);
							sm.closeSession();
						} else {
							throw new BinaryManagerException("No binary data in job");
						}
						break;
					}
				} else {
					throw new BinaryManagerException("Unknown operation in binary manager job");
				}
			} else {
				throw new BinaryManagerException("Binary manager is not available on server");
			}
		} catch (Exception ex) {
			getLogger().error("Error during binary manager process", ex);
			result.setException(ex);
		}
		return result;
	}

}
